package tree;


import java.util.*;

public class Main {
    public static TreeNode buildTree(int[] inorder, int[] postorder) {
        return buildTree(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
    }

    public static TreeNode buildTree(int[] inorder, int[] postorder, int inx, int iny, int pox, int poy) {
        if (inx > iny || pox > poy) {
            return null;
        }
        // 根节点是  po[poy]
        int rootval = postorder[poy];
        TreeNode root = new TreeNode(rootval);
        //回到中序找到根节点
        int rootindexInorder = -1;
        // 这边在寻找中序遍历中间节点的时候 出错了
        for (int i = inx; i <= iny; i++) {
            if (inorder[i] == rootval) {
                rootindexInorder = i;
                break;
            }
        }
        int leftSize = rootindexInorder - inx;
        // 左子树的后序遍历的起点是 po[poy-1-leftsize]
        // 右子树的后序遍历的起点是 po[poy-1] - leftsize - 1
        root.left = buildTree(inorder, postorder, inx, rootindexInorder - 1, pox, pox + leftSize - 1);
        root.right = buildTree(inorder, postorder, rootindexInorder + 1, iny, pox + leftSize, poy - 1);
        return root;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[n];
        int b[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            b[i] = sc.nextInt();
        }
        TreeNode treeNode = buildTree(b, a);
        //进行层次的
        Queue<TreeNode> q = new LinkedList<>();
        q.add(treeNode);
        List<Integer> list = new ArrayList<>();
        while (!q.isEmpty()) {
            TreeNode node = q.poll();
            list.add(node.val);
            if (node.left != null) {
                q.add(node.left);
            }
            if (node.right != null) {
                q.add(node.right);
            }
        }
        for (int i = 0; i < list.size()-1; i++) {
            System.out.print(list.get(i) +  " ");
        }
        System.out.print(list.get(list.size()-1));
    }

    static class TreeNode {

        public Integer val;
        public TreeNode left;

        public TreeNode right;

        public TreeNode(Integer val) {
            this.val = val;
        }

        public TreeNode() {

        }

        public TreeNode(Integer val, TreeNode left,TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}